package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreFunction;
import com.dingreading.cloud.admin.entity.table.StoreFunctionTableDef;
import com.dingreading.cloud.admin.mapper.StoreFunctionMapper;
import com.dingreading.cloud.admin.service.StoreFunctionService;
import com.dingreading.cloud.admin.util.AdminEntityUtils;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-10-11
 */
@Service
public class StoreFunctionServiceImpl extends BaseServiceImpl<StoreFunctionMapper, StoreFunction, StoreFunctionTableDef> implements StoreFunctionService {

    @Override
    protected StoreFunctionTableDef getTable() {
        return StoreFunctionTableDef.storeFunction;
    }

    @Override
    public R<Object> pageList(PageUtil pageUtil, String headStaffUid, String headStaffName, String keyword, Integer status, Integer enabled) {
        if (StringUtils.isBlank(headStaffUid))
            return R.fail("请选择总部员工");
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));
        if (status != null)
            condition.and(table.status.eq(status));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());
        Page<StoreFunction> paginate = getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
        PageDto<StoreFunction> pageDto = new PageDto<>();
        pageDto.setList(paginate.getRecords());
        pageDto.setTotal(paginate.getTotalRow());
        return R.ok(pageDto);
    }

    @Override
    public R<Object> add(String headStaffUid, String headStaffName, String name, String identify, Integer status, String uid) {
        if (StringUtils.isBlank(headStaffUid))
            return R.fail("请选择总部员工");
        if (StringUtils.isBlank(name))
            return R.fail("请填写功能名称");
        if (StringUtils.isBlank(identify))
            return R.fail("请填写功能标识");
        if (status == null || status < 0 || status > 1)
            return R.fail("请选择功能状态");

        StoreFunction storeFun = getMapper().selectOneByCondition(table.identify.eq(identify));
        if (storeFun != null) {
            if (StringUtils.isBlank(uid) || !storeFun.getUid().equals(uid)) {
                return R.fail("功能标识已存在");
            }
        }

        StoreFunction storeFunction = null;
        if (StringUtils.isNotBlank(uid)) {
            storeFunction = getMapper().selectOneByCondition(table.uid.eq(uid));
            if (storeFunction == null) {
                return R.fail("需要更新的功能不存在！");
            }
        }

        storeFunction = AdminEntityUtils.addEntity(storeFunction, headStaffUid, headStaffName, name, identify, status);
        boolean b = saveOrUpdate(storeFunction);
        return b ? R.ok() : R.fail("操作失败");
    }

    @Override
    public R<Object> updateEnabled(String headStaffUid, String headStaffName, String uid, Integer enabled) {
        if (StringUtils.isBlank(headStaffUid))
            return R.fail("请选择总部员工");
        if (StringUtils.isBlank(uid))
            return R.fail("请选择功能");
        if (enabled == null || enabled < 0 || enabled > 1)
            return R.fail("请选择功能启用状态");
        StoreFunction storeFunction = getMapper().selectOneByCondition(table.uid.eq(uid));
        storeFunction.setEnabled(enabled);
        storeFunction.setLastStaffUid(headStaffUid);
        storeFunction.setLastStaffName(headStaffName);
        boolean b = saveOrUpdate(storeFunction);
        return b ? R.ok() : R.fail("更新失败");
    }

    @Override
    public R<Object> get(String headStaffUid, String headStaffName, String uid) {
        if (StringUtils.isBlank(headStaffUid))
            return R.fail("请选择总部员工");
        if (StringUtils.isBlank(uid))
            return R.fail("请选择功能");
        StoreFunction storeFunction = getMapper().selectOneByCondition(table.uid.eq(uid));
        return R.ok(storeFunction);
    }
}
